import plugin from '../../../lib/plugins/plugin.js'
import { segment } from "oicq";
import cfg from '../../../lib/config/config.js'

let answers = [
  "Yes. 是。",
  "No. 不。",
  "Maybe. 或许。",
  "Very likely. 很可能。",
  "Not likely. 不太可能。",
  "It is certain. 肯定是。",
  "It is impossible. 不可能是。",
  "You can count on it. 可以相信。",
  "Don't count on it. 不要相信。",
  "It is doubtful. 存疑。",
  "No doubt about it. 毫无疑问。",
  "The answer is unclear. 答案不明确。",
  "Ask again later. 稍后再问。",
  "Better not to tell you now. 现在最好不告诉你。",
  "Concentrate and ask again. 集中精神再问一次。",
  "Go for it. 就去做吧。",
  "Don't do it. 不要做。",
  "Trust your instincts. 相信你的直觉。",
  "Seek other options. 寻求其他选择。",
  "You have nothing to worry about. 你没有什么可担心的。",
  "You have everything to worry about. 你有很多事情要担心。",
  "The outlook is good. 前景很好。",
  "The outlook is not so good. 前景不太好。",
  "The stars are not aligned. 星辰不利。",
  "The stars are in your favor. 星辰有利。",
  "You will be pleasantly surprised. 你会惊喜地发现。",
  "You will be disappointed. 你会失望。",
  "You will find what you're looking for. 你会找到你想要的东西。",
  "You will lose what you have. 你会失去你所拥有的东西。",
  "You will regret this decision. 你会后悔这个决定。",
  "You will be proud of this decision. 你会为这个决定感到自豪。",
  "You will laugh about this someday. 你总有一天会笑着回想这件事。",
  "You will cry about this someday. 你总有一天会哭着回想这件事。",
  "You will learn something valuable. 你会学到一些有价值的东西。",
  "You will make a mistake. 你会犯一个错误。",
  "You will make a difference. 你会有所作为。",
  "You will make a fool of yourself. 你会出洋相。",
  "You will have a lot of fun. 你会玩得很开心。",
  "You will have a lot of trouble. 你会遇到很多麻烦。",
  "You will have a lot of money. 你会有很多钱。",
  "You will have a lot of debt. 你会有很多债务。",
  "You will have a lot of friends. 你会有很多朋友。",
  "You will have a lot of enemies. 你会有很多敌人。",
  "You will have a long life. 你会长寿。",
  "You will have a short life. 你会短命。"
];

function randomAnswer(array) {
  let length = array.length;
  let index = Math.floor(Math.random() * length);
  return array[index]; 
}

export class example extends plugin {
  constructor () {
    super({
      name: '[Xs插件]答案之书', 
      dsc: '简单开发示例',
      event: 'message',
      priority: -114514,
      rule: [
        {
          reg: "^#答案之书(.*)$", 
          fnc: 'answersbook'
        },
      ]
    })
    this.questions = null; 
    this.timeout = null; 
    this.waitingTime = cfg.waitingTime || 60000; 
    this.timer = null; 
    this.startTimer(); 
  }

  async answersbook (e) {
    if (/#答案之书/.test(e.msg)) {
     e.msg = e.msg.replace(/#答案之书/g, '');
      if (!e.msg) {
        this.setContext('hei');
        e.reply("请发送你要问的问题",true);
         this.timer = setTimeout(() => {
         this.questions = null;           
         this.timer = null;        
         }, this.waitingTime); 
     } else { 
       let msg = e.msg;
       let question = msg; 
       let answer = randomAnswer(answers); 
       let reply = "问题：" + question + "\n" + "答案：" + answer;
        await this.e.reply(reply,true); 
      } 
    }
  }

  startTimer () { 
    this.timer = setInterval(() => { 
    let now = Date.now(); 
      for (let user_id in this.timeout) { 
        if (this.timeout < now) { 
          this.e.reply(segment.at(user_id) + segment.text("你的问题已经超时，请重新发送#答案之书。")); //发送超时消息给用户
          this.questions = null; 
          this.timeout = null; 
        }
      }
    }, 1000);
  }

  stopTimer () { 
    clearInterval(this.timer); 
    this.timer = null; 
  }

  async hei(e) {
    let msg = this.e.msg;
    let question = msg;
    let answer = randomAnswer(answers); 
    let reply = "问题:" + question + "\n" + "答案:" + answer; 
     await this.e.reply(reply,true)
    this.finish('hei')
  }
}